package sk.tuke.homescreen.persistency;

import java.util.ArrayList;
import java.util.List;

import sk.tuke.homescreen.AssignmentPair;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class InternalDAO extends SQLiteOpenHelper{
	  public static final String TABLE_SESSION = "qpassignments";
	  public static final String COLUMN_APPNAME = "_login";
	  public static final String COLUMN_COUNT = "_passHash";

	  private static final String DATABASE_NAME = "launcher.db";
	  private static final int DATABASE_VERSION = 1;

	  private static final String DATABASE_CREATE = "create table "
	      + TABLE_SESSION + "(" + COLUMN_COUNT
	      + " intprimary key not null, " + COLUMN_APPNAME
	      + " text not null);";
	  
	  private SQLiteDatabase mDatabase;

	  public InternalDAO(Context context) {
		    super(context, DATABASE_NAME, null, DATABASE_VERSION);
	  }
			
		@Override
		public void onCreate(SQLiteDatabase database) {
			database.execSQL(DATABASE_CREATE);		
		}
	
		@Override
		public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
			database.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSION);
		    onCreate(database);		
		}
		
		public void open() throws SQLException {
		    mDatabase = getWritableDatabase();
		  }
	
	  public void close() {
	    mDatabase.close();
	  }
	  
	  public String getAppnameByCount(int counts){
		  open();
		  String appname = null;
		  Cursor mDbCursor = mDatabase.query(TABLE_SESSION ,
				  new String[]{COLUMN_APPNAME},
				  COLUMN_COUNT+"="+String.valueOf(counts),null,null,null,null);
		  mDbCursor.moveToFirst();
		  while(!mDbCursor.isAfterLast()){
			  appname = mDbCursor.getString(0);
			  mDbCursor.moveToNext();
		  }
		  close();
		  return appname;		  
	  }
	  
	  public int getCountByAppname(String appname){
		  open();
		  int aCount = 0;
		  Cursor mDbCursor = mDatabase.query(TABLE_SESSION ,
				  new String[]{COLUMN_COUNT},
				  COLUMN_APPNAME+"="+appname,null,null,null,null);
		  
		  mDbCursor.moveToFirst();
		  while(!mDbCursor.isAfterLast()){
			  aCount= mDbCursor.getInt(0);
			  mDbCursor.moveToNext();
		  }
		  close();
		  return aCount;		  
	  }
	  
	  public List<AssignmentPair> getAssignments(){
		  open();
		  List<AssignmentPair> assignments = new ArrayList<AssignmentPair>();
		  int count = 0;
		  String appName = null;
		  Cursor mDbCursor = mDatabase.query(TABLE_SESSION ,
				  new String[]{COLUMN_COUNT, COLUMN_APPNAME},null,null,null,null,null);
		  
		  mDbCursor.moveToFirst();
		  while(!mDbCursor.isAfterLast()){
			  count= mDbCursor.getInt(0);
			  appName = mDbCursor.getString(1);
			  assignments.add(new AssignmentPair(appName, count));
			  mDbCursor.moveToNext();
		  }
		  close();
		  return assignments;
	  }
	  
	  public void cleanDatabase(){
		  open();
		  mDatabase.delete(TABLE_SESSION,null, null);
		  close();
	  }
	  
	  public void insertApp(String appname, int count){
		  open();
		  ContentValues valuesToWrite = new ContentValues();
		  valuesToWrite.put(COLUMN_COUNT, count);
		  valuesToWrite.put(COLUMN_APPNAME, appname);
		  
		  mDatabase.insert(TABLE_SESSION, null, valuesToWrite);
		  close();
	  }
	  
	  public void removeApp(int count){
		  open();
		  mDatabase.delete(TABLE_SESSION, COLUMN_COUNT +"="+String.valueOf(count), null);	  
		  close();
	  }

}
